package com.zky.service;

import com.zky.entity.EmergencyInfo;
import com.zky.mapper.EmergencyInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.time.LocalDateTime;

@Service
public class EmergencyInfoService {
    
    @Autowired
    private EmergencyInfoMapper emergencyInfoMapper;
    
    public List<EmergencyInfo> getAllEmergencyInfo() {
        return emergencyInfoMapper.selectAll();
    }
    
    public EmergencyInfo getEmergencyInfoById(Integer id) {
        return emergencyInfoMapper.selectById(id);
    }
    
    public void createEmergencyInfo(EmergencyInfo emergencyInfo) {
        try {
            // 设置默认值
            if (emergencyInfo.getStatus() == null) {
                emergencyInfo.setStatus("DRAFT");
            }
            
            // 设置时间
            LocalDateTime now = LocalDateTime.now();
            
            // 如果发布日期为空，设置为当前时间
            if (emergencyInfo.getPublishDate() == null) {
                emergencyInfo.setPublishDate(now);
            }
            
            // 设置创建和更新时间
            emergencyInfo.setCreatedAt(now);
            emergencyInfo.setUpdatedAt(now);
            
            System.out.println("Creating emergency info: " + emergencyInfo); // 添加日志
            emergencyInfoMapper.insert(emergencyInfo);
            System.out.println("Created emergency info with ID: " + emergencyInfo.getId()); // 添加日志
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("创建应急信息失败: " + e.getMessage());
        }
    }
    
    public void updateEmergencyInfo(EmergencyInfo emergencyInfo) {
        try {
            // 检查记录是否存在
            EmergencyInfo existingInfo = emergencyInfoMapper.selectById(emergencyInfo.getId());
            if (existingInfo == null) {
                throw new RuntimeException("要更新的记录不存在");
            }
            
            // 设置更新时间
            emergencyInfo.setUpdatedAt(LocalDateTime.now());
            
            // 保留原有的创建信息
            emergencyInfo.setCreatedAt(existingInfo.getCreatedAt());
            emergencyInfo.setCreatedBy(existingInfo.getCreatedBy());
            
            System.out.println("Updating emergency info: " + emergencyInfo);
            emergencyInfoMapper.update(emergencyInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新应急信息失败: " + e.getMessage());
        }
    }
    
    public void deleteEmergencyInfo(Integer id) {
        try {
            // 检查记录是否存在
            EmergencyInfo existingInfo = emergencyInfoMapper.selectById(id);
            if (existingInfo == null) {
                throw new RuntimeException("要删除的记录不存在");
            }
            
            System.out.println("Deleting emergency info with ID: " + id);
            emergencyInfoMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除应急信息失败: " + e.getMessage());
        }
    }
} 